package predatorpreysystems;
/*
 * Copyright (c) 2009 Project E7
 *
 * This file is part of PredatorPreySystems
 *
 * PredatorPreySystems is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PredatorPreySystems is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PredatorPreySystems. If not, see <http://www.gnu.org/licenses/>.
 *
 * PredatorPreySystems is a project that demonstrates lotka volterra's predator
 * prey systems.  The project is also meant to demonstrate linear approximation
 * using differential equations and Euler's method.
 **/

import info.gridworld.actor.Actor;
import info.gridworld.actor.Bug;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
import java.awt.Color;

/**
 *
 * @author markvismonte
 */
public class Prey extends Bug{
    private static final double DARKENING_FACTOR = 0.03;
    //Data Members
    private int offspringFrequency;
    private int counter;
    private PredatorPreySystemUtility pps;

    /**
     * Makes a new Prey that extends Bug
     * @param offspringPerTurn - turns for every offspring
     */
    public Prey(int offspringPerTurn)
        {
        super();
        pps = Main.getPpsUtility();
        this.offspringFrequency = offspringPerTurn;
        counter = 0;
        }

    /**
     * Interface for to the method predictFuturePopulation in pps in order to
     * predict tuture prey population
     * @param turns - how many turns in the future to predict to
     * @param stepSize - how far t is travelled in a step
     * @return - an approximation of the future prey population
     */
    public int predictFuturePreyPopulation(int turns, double stepSize)
        {
        return pps.predictFuturePopulation(true, true, pps.getPreyPopulation(),
                pps.getPredatorPopulation(), 0.0, 0.0, 0.0, 0.0, stepSize, 0.0,
                turns);
        }

    @Override
    /**
     * Calls super.act() and increases step counter
     */
    public void act() {
        super.act();
        Color c = getColor();
        int red = (int) (c.getRed() * (1 - DARKENING_FACTOR));
        int green = (int) (c.getGreen() * (1 - DARKENING_FACTOR));
        int blue = (int) (c.getBlue() * (1 - DARKENING_FACTOR));

        setColor(new Color(red, green, blue));
        counter++;
        }

    @Override
    /**
     * Calls super.removeSelfFromGrid() and increments prey decay in pps
     */
    public void removeSelfFromGrid() {
        super.removeSelfFromGrid();
        pps.incrementPreyDecay();
        }

    @Override
    /**
     * Moves the Prey
     */
    public void move()
        {
        Grid<Actor> gr = getGrid();
        if (gr == null)
          return;
        Location loc = getLocation();
        Location next = loc.getAdjacentLocation(getDirection());
        if (gr.isValid(next))
          moveTo(next);
        else
          removeSelfFromGrid();
        if (counter > offspringFrequency)
            {
            (new Prey(offspringFrequency)).putSelfInGrid(getGrid(), loc);
            counter = 0;
            pps.incrementPreyGrowth();
            }
        }

}
